<div id="app">
    <section class="content-header">
        <h1>
            <small>{{$channel['cha_describe']}} - 首页排版</small>
        </h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">

                        <span>


    <a class="btn btn-sm btn-primary grid-refresh" title="刷新"><i class="fa fa-refresh"></i><span class="hidden-xs"> 刷新</span></a>

    <div class="btn-group" style="margin-right: 10px" data-toggle="buttons">
        <label class="btn btn-sm btn-dropbox add-filter-btn" title="添加产品图">
            <input type="checkbox"> <span class="hidden-xs">添加产品图</span>
        </label>
    </div>
  </span>
                    </div>

                    <!--   添加开始   -->
                    <div class="box-header with-border hide" id="add-box">
                        <form action="/admin/lunbo/index" class="form-horizontal" pjax-container method="get">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="box-body">
                                        <div class="fields-group">
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">商品选择</label>
                                                <div class="col-sm-8">
                                                    <select class="form-control  " id="goods_id" name="goods_id" style="width: 100%;">
                                                        <option value="0"></option>
                                                        @if($selectGoods)
                                                            @foreach($selectGoods as $k=>$v)
                                                                <option value="{{$v['goods_id']}}">{{$v['goods_name']}}</option>
                                                            @endforeach
                                                        @endif
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- /.box-body -->

                            <div class="box-footer">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="col-md-2"></div>
                                        <div class="col-md-8">
                                            <div class="btn-group pull-left">
                                                <button type="button" onclick="add({{$channel['cha_id']}})" class="btn btn-info submit btn-sm">添加</button>
                                            </div>
                                            <div class="btn-group pull-left " style="margin-left: 10px;">
                                                <button type="button" class="btn btn-info submit btn-sm">取消</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <!--添加结束-->

                    <!-- /.box-header -->
                    <!--数据列表开始   -->
                    <div class="box-body table-responsive no-padding">
                        <div>
                            <a href="/admin/homePage/index?cha_id={{$cha_id}}"  @if($cate_id==1) style="padding:10px;background: #0d6aad;color:white;line-height: 30px;" @else  style="padding:10px;" @endif >新品推荐</a>

                            @if($category)
                                @foreach($category as $v)
                                    <a href="/admin/homePage/index?cha_id={{$cha_id}}&cate_id={{$v['cate_id']}}" data-id="{{$v['cate_id']}}" @if($cate_id==$v['cate_id']) style="padding:10px;background: #0d6aad;color:white;line-height: 30px;" @else  style="padding:10px;" @endif >{{$v['cate_name']}}</a>
                                @endforeach
                            @endif
                        </div>


                        @if($cate_id>1)
                            <table class="table table-hover" style="margin-bottom: 50px;margin-top:20px;">
                                <caption style="padding:5px;background: #abcdef;color:white;">banner图</caption>
                                <thead >
                                <tr >
                                    <th>图片</th>
                                    <th>跳转链接</th>
                                    <th>操作</th>
                                </tr>
                                </thead>

                                <tbody>

                                @if (!empty($banner))
                                        <tr >
                                            <td >
                                                <img src="{{$url}}/{{$banner['litpic']}}" width="100" />

                                                <button class="imgedit" data-id="{{$banner['id']}}">更换banner</button>
                                            </td>
                                            <td >
                                               <input onblur="editBannerUrl(this)" class="form-control" data-id="{{$banner['id']}}" type="text" value="{{$banner['content_url']}}" >
                                            </td>
                                            <td >
                                                <a class="banner-grid-row-delete"  data-id="{{$banner['id']}}">
                                                    删除
                                                </a>
                                            </td>
                                        </tr>
                                @else
                                    <tr >
                                        <td colspan="3">
                                             <button class="imgedit" data-id="0" >添加banner</button>
                                        </td>
                                    </tr>
                                @endif
                                </tbody>
                            </table>
                        @endif


                        <table class="table table-hover" style="margin-top: 15px;">
                            <caption style="padding:5px;background: #abcdef;color:white;">产品图</caption>
                            <thead >
                            <tr >
                                <th>图片</th>
                                <th>商品名称</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>

                            <tbody>

                            @if (!empty($newGoods))
                                @foreach($newGoods as $v)
                                    <tr >
                                        <td >
                                            <img src="{{$url}}/{{$v['goods_img_list']}}" width="100" />
                                        </td>
                                        <td >
                                            {{$v['goods_name']}}
                                        </td>
                                        <td >
                                             <input onblur="editSort(this)" data-id="{{$v['id']}}" type="text" value="{{$v['sort']}}">
                                        </td>
                                        <td >
                                            <a class="grid-row-delete"  data-id="{{$v['id']}}">
                                                删除
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            @endif
                            </tbody>
                        </table>


                        <input  data-id="0" type="file" style="display:none;" onchange="upimg(this)" name="img" id="upImg">
                    </div>
                    <!--数据列表结束-->


                    <input type="hidden" id="cha_id" value="{{$cha_id}}">
                    <input type="hidden" id="cate_id" value="{{$cate_id}}">

                    <!-- /.box-body -->
                </div>
            </div>
        </div>

    </section>
</div>

<script data-exec-on-popstate>
var s='';

    $(function () {
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });

        $("#goods_id").select2({"allowClear":true,"placeholder":{"id":"","text":"请选择商品"}});

        //banner单条数据删除
        $('.banner-grid-row-delete').unbind('click').click(function() {
            var id = $(this).data('id');
            var cha_id=$("#cha_id").val();
            var cate_id=$("#cate_id").val();
            swal({
                title: "确认删除?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        $.ajax({
                            method: 'POST',
                            url: '/admin/homePage/bannerDelete',
                            data: {
                                _token:LA.token,
                                id:id,
                                cha_id:cha_id,
                                cate_id:cate_id
                            },
                            dataType:'json',
                            success: function (data) {
                                if(data.status==1){
                                    swal(data.message, '', 'success');
                                    $.pjax.reload('#pjax-container');
                                }else{
                                    swal(data.message, '', 'error');
                                }
                                resolve(data);
                            }
                        });
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        swal(data.message, '', 'success');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });
        //单条数据删除
        $('.grid-row-delete').unbind('click').click(function() {
            var id = $(this).data('id');
            console.log(id);
            swal({
                title: "确认删除?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        $.ajax({
                            method: 'POST',
                            url: '/admin/homePage/newGoodsDelete',
                            data: {
                                _token:LA.token,
                                id:id
                            },
                            dataType:'json',
                            success: function (data) {
                                if(data.status==1){
                                    swal(data.message, '', 'success');
                                    $.pjax.reload('#pjax-container');
                                }else{
                                    swal(data.message, '', 'error');
                                }
                                resolve(data);
                            }
                        });
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        swal(data.message, '', 'success');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        //刷新
        $('.grid-refresh').on('click', function() {
            $.pjax.reload('#pjax-container');
            toastr.success('刷新成功 !');
        });

        //添加产品
        $('.add-filter-btn').unbind('click');
        $('.add-filter-btn').click(function (e) {
            if ($('#add-box').is(':visible')) {
                $('#add-box').addClass('hide');
            } else {
                $('#add-box').removeClass('hide');
            }
        });

        $('.imgedit').click(function(e){
            $("#upImg").attr('data-good',$(this).attr('data-good'));
            $("#upImg").attr('data-id',$(this).attr('data-id'));
            $("#upImg").attr('data-type',$(this).attr('data-type'));
            console.log($(this).attr('data-good'));
            $("#upImg").click();
            s=$(this);
        });

    });


function editSort(e){
    var id=$(e).attr('data-id');
    var sort=$(e).val();

    $.ajax({
        url:'/admin/homePage/ajaxEditSort',
        data:{id:id,sort:sort,_token:LA.token},
        type:'post',
        dataType:'json',
        success:function (m) {
            if(m.status==1){
                toastr.success(m.message);
            }else{
                toastr.error(m.message);
            }
        }
    });
}

function editBannerUrl(e){
    var id=$(e).attr('data-id');
    var url=$(e).val();
    if(url==''){
        toastr.error('请填写跳转地址');
    }
    $.ajax({
        url:'/admin/homePage/ajaxEditBannerUrl',
        data:{id:id,url:url,_token:LA.token},
        type:'post',
        dataType:'json',
        success:function (m) {
            if(m.status==1){
                toastr.success(m.message);
            }else{
                toastr.error(m.message);
            }
        }
    });
}

    function upimg(e){
        var id=$(e).attr('data-id');
        var file=e.files[0];
        var cha_id=$("#cha_id").val();
        var cate_id=$("#cate_id").val();

        var data=new FormData();

        data.append('id',id);
        data.append('img',file);
        data.append('cha_id',cha_id);
        data.append('cate_id',cate_id);
        data.append('_token',LA.token);
        $.ajax({
            url:'/admin/homePage/bannerImg',
            data:data,
            type:'post',
            dataType:'json',
            contentType:false,
            processData:false,
            success:function (m) {
                if(m.status==1){
                    toastr.success(m.message);
                    $.pjax.reload('#pjax-container');
                }else{
                    toastr.error(m.message);
                }
            }
        });

    }

function add(id){
    var goods_id=$("#goods_id").val();
    var cha_id=$("#cha_id").val();
    var cate_id=$("#cate_id").val();
    if(goods_id<1){
        toastr.error('请选择商品');
    }
    $.ajax({
        url:"/admin/homePage/addNewGoods",
        type:'post',
        data:{goods_id:goods_id,cate_id:cate_id,cha_id:cha_id,_token:LA.token},
        dataType:'json',
        success:function(m){
            if(m.status==1){
                toastr.success(m.message);
                $.pjax.reload('#pjax-container');
            }else{
                swal(m.message, '', 'error');
            }
        }
    });
}

</script>